package com.interfaces;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.entity.Collector;

/**
 * @Title: CollectorDao.java
 * @Package com.interfaces
 * @author 孙梦楠
 * @date 2017年7月19日 下午3:24:49
 * @version V1.0.0
 */

public interface CollectorDao extends JpaRepository<Collector, Integer> {

	// 条件列表，使用ISNULL限定实现拼接查询
	// 使用（）限定单个条件，使用and连接多个条件
	@Transactional
	@Query(value = "SELECT c FROM Collector c WHERE (c.collectorcode=?1 OR ?1 IS NULL )"
			+ "AND (c.collectorassetcode=?2 OR ?2 IS NULL )" + "AND (c.collectortype=?3 OR ?3 IS NULL)"
			+ "AND (c.collectorprotocol=?4 OR ?4 IS NULL)" + "AND (c.collectormade=?5 OR ?5 IS NULL)"
			+ "AND (c.collectorstatus=?6 OR ?6 IS NULL) AND  (c.collectororg=?7 OR ?7 IS NULL) AND (c.type=?8)")
	public List<Collector> findByConditions(String collectorcode, String collectorassetcode, String collectortype,
			String collectorprotocol, String collectormade, String collectorstatus, Integer collectororg,String type);

	

	Collector findByCollectorcodeAndCollectorstatusNot(String collectorcode,String collectorstatus);

	Collector findByCollectorassetcodeAndCollectorstatusNot(String collectorassetcode,String collectorstatus);

	List<Collector> findByCollectororgAndTypeAndCollectorstatusNot(Integer collectororg,String type,String collectorstatus);
	List<Collector> findByTypeAndCollectorstatusNot(String type,String collectorstatus);

	List<Collector> findDistinctCollectorByCollectortypeAndCollectorstatusNot(String collectortype,String collectorstatus);
	
	//更新采集器状态
		@Transactional
		@Modifying
		@Query("update Collector c set c.collectorstatus = ?1 where c.collectorid = ?2") 
		int updateCollector(String collectorstatus,Integer collectorid);
	
		
		

}
